<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="expires" content="no-cache">
<title>Insert title here</title>
<%
	//防止浏览器产生缓存
	response.setHeader("Pragma","No-Cache"); 
	response.setHeader("Cache-Control","No-Cache"); 
	response.setDateHeader("Expires", 0); 

	//定义基础路径，从DispatcherServlet获取数据
	String path = request.getContextPath() + "/";	
	String resourcesPath = path + "resources/";
	String omPath = resourcesPath + "operamasks-ui-2.0/";		
	
%>
<link href="<%=omPath%>css/default/om-default.css" rel="stylesheet"	type="text/css" />
<script src="<%=omPath%>js/jquery.min.js" type="text/javascript"></script>
<script src="<%=path%>resources/sm/js/jquery.xml2json.js" type="text/javascript"></script>
<script src="<%=omPath%>js/operamasks-ui.min.js" type="text/javascript"></script>
<script src="<%=path%>resources/sm/js/om-dialog-url.js" type="text/javascript"></script>
<style type="text/css">
	.search-button{
	padding-left: 30px;
	padding-top: 9px;
	}
	
   	.myinput input{
    border:1px solid;
    border-color:lightblue;
    height: 18px;
    width : 160px;
   	}
   	.myinput input:focus{border: 1px solid #3A76C2;}
 	.mytable td, .mylabel{
   	padding-top: 5px;
   	}
   .mytable .mytable-left{
   text-align: right;
   width : 100px;
   }
  .mytable .mytable-right{
   width : 150px;
   }
</style>
<script type="text/javascript">
//集中获取ModelAndView数据
var path = "${pageContext.request.contextPath}" + "/";
var deliveryType = "调拨";//定义进厂类型
//定义全局变量
var systemtips = null;// 生成系统提示对话框
var grid = null; // 生成表格
var searchFiled = null;// 生成搜索字段
var serchButton = null;// 生成搜索按钮
var mainTool = null;// 生成工具栏
var dialogSingle = null;// 生成收货对话框
var single = null;

// 设置表格操作提交的网址
var searchUrl = path + "rm/filterQuery.html";
var matReceiveUrl = path + "rm/MatReceiveCtl/matReceive.html";
var matReturnUrl = path + "rm/MatReturnCtl/matReturn.html";


$(function() {
	initVars();
	loadLayout();// 加载布局
	loadGrid();// 加载表格
	loadSearchPanel();// 加载搜索面板
	loadButtonBar();// 加载工具栏
	initDialogSingle();
	search();// 搜索
});

function initVars(){
	// 集中定义jQuery选择器
	systemtips = $('#systemtips');// 生成系统提示对话框
	grid =$('#maingrid'); // 生成表格
	searchFiled = $('#search-field');// 生成搜索字段
	serchButton = $('#search-button'); // 生成搜索按钮
	mainTool = $('#maintool'); // 生成工具栏
	dialogSingle = $("#dialog-matReceiveSingle"); // 生成收货对话框
	single = $("#Single");
}

function loadLayout(){
	$('#page').omBorderLayout({
		panels : [ {
			id : "center-panel",
			header : false,
			region : "center"
		} ]
	});
	$('#search-panel').omPanel({
		title : "搜索",
		collapsible : true,
		height : 155
	});
}

function loadGrid(){
	grid.omGrid(
			{
				limit: 30,
				singleSelect: false,
		        title : '原料进厂数据',
		        height:500,
		        colModel : [
						{header : '计量单号', name : 'measureId', align : 'center', width : '70', sort:'serverSide'},
						{header : '物料编码', name : 'materialId', align : 'center', width : '70', sort:'serverSide'},
						{header : '物料描述', name : 'materialDesc', align : 'center', width : '70', sort:'serverSide'},
						{header : '毛重', name : 'grossWeight', align : 'center', width : '70', sort:'serverSide'},
						{header : '皮重', name : 'tareWeight', align : 'center', width : '70', sort:'serverSide'},
						{header : '净重', name : 'netWeight', align : 'center', width : '70', sort:'serverSide'},
						{header : '主车号', name : 'cartNumber', align : 'center', width : '70', sort:'serverSide'},
						{header : '采购订单', name : 'purchaseOrder', align : 'center', width : '70', sort:'serverSide'},
						{header : '收货时间', name : 'receiveTime', align : 'center', width : '70', sort:'serverSide'},
						{header : '收货状态', name : 'status', align : 'center', width : '70', sort:'serverSide'},
						{header : '进厂类型', name : 'deliveryType', align : 'center', width : '70', sort:'serverSide'},
		                    ],
				onBeforeEdit : function(){
				},
				onAfterEdit : function(){
				},
				onCancelEdit : function(){
				}
			}
			);
}

//加载搜索面板
function loadSearchPanel() {
	$('#search-field input').each(function() {
		$(this).keyup(function(event) {
			if (event.keyCode == 13) {
				search();
			}
		});
	});
	
	$('#queryStatus').omCombo({
		dataSource : [ {
			text : '未收货',
			value : 'IN'
		}, {
			text : '已收货',
			value : 'RECEIVE'
		}, {
			text : '已退货',
			value : 'RETURN'
		} ],
		editable : false,
		inputField:function(data,index){
		    return data.text+'('+data.value+')';
		},
		value : 'IN',
		width : 160,
	});
	$("#queryTimeStampS").omCalendar({showTime : true});
	$("#queryTimeStampE").omCalendar({showTime : true});
	$("#queryTimeStampS").css("width","140px");
	$("#queryTimeStampE").css("width","140px");
	// 加载搜索按钮
	serchButton.omButton({
		icons : {
			left : path + 'resources/sm/images/search.png'
		},
		width : 70,
		onClick : function(event) {
			if ($("#queryStatus", searchFiled).val() == "Y") {
				mainTool.hide();
			} else {
				mainTool.show();
			}
			search();
		}
	});
}

function loadButtonBar() {
	var buttons = [];
	var btn = {
		id : "matRcv",
		label : "收货",
		onClick : function() {
			var selections = grid.omGrid('getSelections', true);
			if (selections.length == 1) {
				showDialogSingle("收货确认", selections[0]);// 显示dialog;
			} else {
				$.omMessageBox.alert({
					content : '请选择一行记录',
				});
			}
		}
	};
	buttons.push(btn);
	btn = {
		id : "matRtn",
		label : "退货",
		onClick : function() {
			var selections = grid.omGrid('getSelections', true);
			if (selections.length == 1) {
				showDialogRtn(selections[0]);
			} else {
				$.omMessageBox.alert({
					content : '请选择一行记录',
				});
			}
		}
	};
	buttons.push(btn);
	mainTool.omButtonbar({
		btns : buttons
	});
}

function search()
{	
	var rules =[], i=0;
	$(':input','#searchTable').each(function() {
		if($.trim(this.name)!="")
			if( $(this).val() != null &&  $.trim($(this).val()) != "" 
					&& typeof($(this).attr('op')) != "undefined" && typeof($(this).attr('ty')) != "undefined")
				rules[i++] = {op : $(this).attr('op'), field : $(this).attr('name'), value : $(this).val(), type : $(this).attr('ty')};
	});
	rules[i++] = {op : 'equal', field : 'deliveryType', value : deliveryType, type : 'string'};
	var group ={
			op : "and",
			rules : rules
	};
	var extraData = {
			"domain" : "SmRmMeasureDoc",
			"rules" : i==0?null:JSON.stringify(group),
	};
	grid.omGrid({
		dataSource : searchUrl,
		extraData : extraData, 
		method:'POST'
	}, 'reload');
	var state = $('#queryStatus', searchFiled).omCombo('value');
	if (state == "IN") {
		$("#matRcv", mainTool).parent().parent().show();
		$("#matRtn", mainTool).parent().parent().hide();
	} else if (state == "RECEIVE") {
		$("#matRcv", mainTool).parent().parent().hide();
		$("#matRtn", mainTool).parent().parent().show();
	} else if (state == "RETURN") {
		$("#matRcv", mainTool).parent().parent().hide();
		$("#matRtn", mainTool).parent().parent().hide();
	}
}

function initDialogSingle() {
	
	var buttons = [ {
		text : "确定",
		click : function() {
			matReceive();
		}
	}, {
		text : "取消",
		click : function() {
			dialogSingle.omDialog("close");// 关闭dialog
			grid.omGrid('reload');// 刷新表格数据
		}
	} ];

	dialogSingle.omDialog({
		width : 800,
		height : 270,
		autoOpen : false,
		resizable : false,
		modal : true,
		buttons : buttons
	});
	// 显示dialog并初始化里面的输入框的数据
	showDialogSingle = function(title, rowData) {
		$(':input',single).each(function(){
	    	$(this,dialogSingle).val(rowData[this.name]);
		});
		dialogSingle.omDialog("option", "title", title);
		dialogSingle.omDialog("open");// 显示dialog
	};

}

function showDialogRtn(rowData) {
	var msg = "确认对计量单<b>";
	msg += rowData.measureId;
	msg += "</b>退货？";
	$.omMessageBox.confirm({
		title : '退货确认',
		content : msg,
		onClose : function(go) {
			if (go) {
				var info = {
						"measureId" : rowData.measureId,
						"deliveryType" : deliveryType
				};
				$.post( matReturnUrl,info, 
						function(data, textStatus)
						{
					     if( data.success )
							{
					    	 $.omMessageBox.alert( { type: "success", title:"成功", content: data.message } );
					    	 grid.omGrid( "reload" );
							}
					     else
					    	 {
								$.omMessageBox.alert( { type: "error", title:"失败", content: data.message } );
					    	 }						
						});
			}
		}
	});
}

function getSubmitData(form)
{
	var info = {};
	$(':input',form).each(function(){
		if(typeof($(this).attr('id')) != "undefined" && typeof($(this).attr('name')) != "undefined"){
			info[$(this).attr('id')] = $(this).val();
		}
	});

	return JSON.stringify(info);
}

function matReceive(){
	
	var info = {
			"SmRmMeasureDoc" : getSubmitData(dialogSingle)
	};
	$.post( matReceiveUrl,info, 
			function(data, textStatus)
			{
		     if( data.success )
				{
		    	 $.omMessageBox.alert( { type: "success", title:"成功", content: data.message } );
		    	 grid.omGrid( "reload" );
				}
		     else
		    	 {
					$.omMessageBox.alert( { type: "error", title:"失败", content: data.message } );
		    	 }						
			});
	dialogSingle.omDialog("close");
}

</script>
</head>
<body>	
	<div id="page" style="height:690px;">
		<div id="center-panel">
			<div id="search-panel"> 
				<div id="search-field">
				<table id="searchTable" class="mytable">
					<tr>
						<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">进厂类型:</font></span></td>
						<td class="mytable-right"><span class="myinput"><input id="queryDeliveryType" name="deliveryType" op="equal" ty="string" type="text" value="调拨" disabled="disabled" /></span></td>
						<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">计量单号:</font></span></td>
						<td class="mytable-right"><span class="myinput"><input id="queryMeasureId" name="measureId" op="startwith" ty="string" type="text" /></span></td>
						<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">采购订单:</font></span></td>
						<td class="mytable-right"><span class="myinput"><input id="queryPurchaseOrder" name="purchaseOrder" op="startwith" ty="string" type="text" /></span></td>
						<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">物料编码:</font></span></td>
						<td class="mytable-right"><span class="myinput"><input id="queryMaterialId" name="materialId" op="startwith" ty="string" type="text" /></span></td>
					</tr>
					<tr>
						<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">主车号:</font></span></td>
						<td class="mytable-right"><span class="myinput"><input id="queryCartNumber" name="cartNumber" op="like" ty="string" type="text" /></span></td>
						<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">收货状态:</font></span></td>
						<td class="mytable-right"><span class="myinput"><input id="queryStatus" name="status" op="equal" ty="string" type="text" /></span></td>
						<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">接收时间从:</font></span></td>
						<td class="mytable-right"><span class="myinput"><input id="queryTimeStampS" name="timeStamp" op="greaterorequal" ty="date" type="text" /></span></td>
						<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">接收时间到:</font></span></td>
						<td class="mytable-right"><span class="myinput"><input id="queryTimeStampE" name="timeStamp" op="lessorequal" ty="date" type="text" /></span></td>
					</tr>
				</table>
				</div>		
				<div class="search-button"><span id="search-button">搜索</span></div>				
			</div>
			<div id="maintool"></div>
			<table id="maingrid"></table>
		</div>
	</div>
	<div id="dialog-matReceiveSingle">
		<form id="Single">
		<table class="mytable">
			<tr>
				<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">计量单号:</font></span></td>
				<td class="mytable-right"><span class="myinput"><input id="measureId" name="measureId"  type="text" readonly="readonly" /></span></td>
				<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">采购订单:</font></span></td>
				<td class="mytable-right"><span class="myinput"><input id="purchaseOrder" name="purchaseOrder"  type="text" readonly="readonly" /></span></td>
				<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">主车号:</font></span></td>
				<td class="mytable-right"><span class="myinput"><input id="cartNumber" name="cartNumber"  type="text" readonly="readonly" /></span></td>
			</tr>
			<tr>
				<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">物料编码:</font></span></td>
				<td class="mytable-right"><span class="myinput"><input id="materialId" name="materialId"  type="text" readonly="readonly" /></span></td>
				<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">物料描述:</font></span></td>
				<td class="mytable-right"><span class="myinput"><input id="materialDesc" name="materialDesc"  type="text" readonly="readonly" /></span></td>
				<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">收货状态:</font></span></td>
				<td class="mytable-right"><span class="myinput"><input id="status" name="status"  type="text" readonly="readonly" /></span></td>
			</tr>
			<tr>
				<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">进厂类型:</font></span></td>
				<td class="mytable-right"><span class="myinput"><input id="deliveryType" name="deliveryType"  type="text" readonly="readonly" /></span></td>
				<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">接收时间:</font></span></td>
				<td class="mytable-right"><span class="myinput"><input id="timeStamp" name="timeStamp"  type="text" readonly="readonly" /></span></td>
			</tr>
		</table>
		<div class="mylabel"><label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;重量信息(KG)</label></div>
		<table class="mytable">
			<tr>
				<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;毛重:</font></span></td>
				<td class="mytable-right"><span class="myinput"><input id="grossWeight" name="grossWeight"  type="text" readonly="readonly" /></span></td>
				<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;皮重:</font></span></td>
				<td class="mytable-right"><span class="myinput"><input id="tareWeight" name="tareWeight"  type="text" readonly="readonly" /></span></td>
				<td class="mytable-left"><span><font face="微软雅黑" size="2px" color="darkblue">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;净重:</font></span></td>
				<td class="mytable-right"><span class="myinput"><input id="netWeight" name="netWeight"  type="text" readonly="readonly" /></span></td>
			</tr>
		</table>
		</form>
	</div>
	<div id="systemtips" title="系统提示信息"></div>
</body>
</html>